/**
 * Settle模块 - 子商户管理API
 * @description 子商户管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type {
  SubMerchantVO,
  SubMerchantCreateReqVO,
  SubMerchantPageReqVO,
  PageResult
} from './types'

/**
 * 获取子商户分页列表
 * @param params 分页查询参数
 * @returns 子商户分页数据
 */
export const getSubMerchantPageApi = (
  params: SubMerchantPageReqVO
): Promise<PageResult<SubMerchantVO>> => {
  return request.get({ url: '/settle/sub-merchant/page', params })
}

/**
 * 获取子商户详情
 * @param id 子商户ID
 * @returns 子商户详情
 */
export const getSubMerchantApi = (id: number): Promise<SubMerchantVO> => {
  return request.get({ url: `/settle/sub-merchant/get?id=${id}` })
}

/**
 * 创建子商户
 * @param data 子商户创建数据
 * @returns 创建结果
 */
export const createSubMerchantApi = (data: SubMerchantCreateReqVO): Promise<number> => {
  return request.post({ url: '/settle/sub-merchant/create', data })
}

/**
 * 更新子商户
 * @param data 子商户更新数据
 * @returns 更新结果
 */
export const updateSubMerchantApi = (
  data: SubMerchantCreateReqVO & { id: number }
): Promise<boolean> => {
  return request.put({ url: '/settle/sub-merchant/update', data })
}

/**
 * 删除子商户
 * @param id 子商户ID
 * @returns 删除结果
 */
export const deleteSubMerchantApi = (id: number): Promise<boolean> => {
  return request.delete({ url: `/settle/sub-merchant/delete?id=${id}` })
}

/**
 * 审核子商户
 * @param id 子商户ID
 * @param status 审核状态: 1-审核通过 2-审核拒绝
 * @param remark 审核备注
 * @returns 审核结果
 */
export const auditSubMerchantApi = (
  id: number,
  status: number,
  remark?: string
): Promise<boolean> => {
  return request.put({
    url: '/settle/sub-merchant/audit',
    data: { id, status, remark }
  })
}

/**
 * 获取主商户下的子商户列表
 * @param merchantNo 主商户编号
 * @returns 子商户列表
 */
export const getSubMerchantByMerchantApi = (merchantNo: string): Promise<SubMerchantVO[]> => {
  return request.get({ url: `/settle/sub-merchant/list-by-merchant?merchantNo=${merchantNo}` })
}

/**
 * 批量审核子商户
 * @param ids 子商户ID列表
 * @param status 审核状态: 1-审核通过 2-审核拒绝
 * @param remark 审核备注
 * @returns 批量审核结果
 */
export const batchAuditSubMerchantApi = (
  ids: number[],
  status: number,
  remark?: string
): Promise<boolean> => {
  return request.put({
    url: '/settle/sub-merchant/batch-audit',
    data: { ids, status, remark }
  })
}

/**
 * 导出子商户数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportSubMerchantApi = (params: SubMerchantPageReqVO): Promise<void> => {
  return request.download({
    url: '/settle/sub-merchant/export',
    params
  })
}
